package dao;

import pojo.Role;
import pojo.Student;
import pojo.Users;
import utils.DbUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UsersDaoImpl extends DbUtils implements UsersDao{
    @Override
    public Users selectUsersByName(String loginName) throws SQLException {
        String sql = "select * from users where loginname = ?";
        List list = new ArrayList();
        list.add(loginName);
        ResultSet query = query(sql, list);
        Users users = new Users();
        while (query.next()){
            users.setLoginName(query.getString("loginname"));
            users.setPassword(query.getString("password"));
            users.setRealName(query.getString("realname"));
            users.setRoleId(query.getInt("roleid"));
            users.setUserid(query.getInt("userid"));
        }
        closeAll();
        return users;
    }

    @Override
    public List<Users> selectAll(Integer pageIndex, Integer pageSize) throws SQLException {
        String sql = "select userid,loginname,realname,rolename from users,role where users.roleid=role.roleid  limit ?,?";
        List param = new ArrayList();
        List<Users> usersList = new ArrayList<>();
        param.add((pageIndex-1)*pageSize);
        param.add(pageSize);
        ResultSet query = query(sql, param);
        while (query.next()){
            Users users = new Users();
            Role role = new Role();
            users.setUserid(query.getInt("userid"));
            users.setLoginName(query.getString("loginname"));
            users.setRealName(query.getString("realname"));
            role.setRoleName(query.getString("rolename"));
            users.setRole(role);
            usersList.add(users);
        }
        closeAll();
        return usersList;
    }

    @Override
    public int total() throws SQLException {
        String sql = "select count(*) from users";
        int total = 0;
        ResultSet query = query(sql, null);
        while (query.next()){
            total = query.getInt(1);
        }
        closeAll();
        return total;
    }

    @Override
    public int addUser(Users user) {
        String sql = "insert into users values(?,?,?,?,?,?,?,?,?,?,?)";
        List list = new ArrayList<>();
        list.add(user.getUserid());
        list.add(user.getLoginName());
        list.add(user.getPassword());
        list.add(user.getRealName());
        list.add(user.getSex());
        list.add(user.getEmail());
        list.add(user.getAddress());
        list.add(user.getPhone());
        list.add(user.getCardId());
        list.add(user.getDesc());
        list.add(user.getRoleId());
        int update = update(sql, list);
        return update;
    }

    @Override
    public int updateUser(Users user) {
        String sql = "update users set loginname=?,password=?,realname=?," +
                "sex=?,email=?,address=?,phone=?,cardid=?," +
                "users.desc=?,roleid=? where userid=?";
        List list = new ArrayList();
        list.add(user.getLoginName());
        list.add(user.getPassword());
        list.add(user.getRealName());
        list.add(user.getSex());
        list.add(user.getEmail());
        list.add(user.getAddress());
        list.add(user.getPhone());
        list.add(user.getCardId());
        list.add(user.getDesc());
        list.add(user.getRoleId());
        list.add(user.getUserid());
        int update = update(sql, list);
        return update;
    }

    @Override
    public Users selectById(int uid) {
        String sql = "select * from users where userid=?";
        List list = new ArrayList();
        list.add(uid);
        ResultSet query = query(sql, list);
        Users users = new Users();
        try {
            while (query.next()){
                users.setUserid(query.getInt("userid"));
                users.setLoginName(query.getString("loginname"));
                users.setPassword(query.getString("password"));
                users.setRealName(query.getString("realname"));
                users.setSex(query.getInt("sex"));
                users.setEmail(query.getString("email"));
                users.setAddress(query.getString("address"));
                users.setPhone(query.getString("phone"));
                users.setCardId(query.getString("cardid"));
                users.setDesc(query.getString("desc"));
                users.setRoleId(query.getInt("roleid"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return users;
    }

    @Override
    public int deleteById(int uid) {
        String sql = "delete from users where userid=?";
        List list = new ArrayList();
        list.add(uid);
        int update = update(sql, list);
        closeAll();
        return update;
    }

    @Override
    public Users selectByuName(String loginname) {
        return null;
    }
}
